package com.fei.wechat.admin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fei.wechat.common.constant.Constants;
import com.fei.wechat.common.model.vo.RespVo;
import com.fei.wechat.middlewar.mode.entity.admin.Authority;
import com.fei.wechat.middlewar.mode.vo.admin.CurrentUser;
import com.fei.wechat.middlewar.mode.vo.admin.MenuVo;

/**
 * 描述: Home控制层
 * 版权: Copyright (c) 2018 
 * 公司: 飞翔科技 
 * 作者: 张进飞 
 * 版本: 1.0 
 * 创建日期: 2018年5月20日 
 * 创建时间: 上午11:33:53
 */
@RequestMapping("/admin/home")
@Controller
public class HomeController extends BaseController
{
    
    /**
     * 描述：首页框架
     * 作者：张进飞
     * 时间：2018年5月20日 上午11:50:18
     * @param request1
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request1)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("index");
        return mav;
    }
    
    /**
     * 描述：主页
     * 作者：张进飞
     * 时间：2018年6月16日 下午3:03:46
     * @param request1
     * @return
     */
    @RequestMapping("/main")
    public ModelAndView homeMain(HttpServletRequest request1)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("main");
        return mav;
    }
    
    /**
     * 描述：获取当前用户菜单
     * 作者：张进飞
     * 时间：2018年6月10日 下午2:33:48
     * @param request1
     * @return
     */
    @RequestMapping("/queryMenus")
    @ResponseBody
    public RespVo queryMenus(HttpServletRequest request1)
    {
        RespVo respVo = new RespVo();
        
        respVo.setCode(RespVo.CODE_SUCCESS);
        respVo.setMessage("查询菜单成功");
        respVo.setResult(this.getMenuLists(request1));
        return respVo;
    }
    
    /**
     * 描述：获取菜单列表
     * 作者：张进飞
     * 时间：2018年6月16日 下午2:27:11
     * @param request1
     * @return
     */
    private List<Authority> getMenuLists(HttpServletRequest request1)
    {
        CurrentUser user = (CurrentUser) request1.getSession().getAttribute(Constants.USER_LOGIN_FLAG);
        if (null == user)
        {
            return null;
        }
        List<Authority> currentAuthority = new ArrayList<Authority>();
        List<Authority> authorityList = user.getAuthoritys();
        //根据parentId对菜单进行分类
        Map<Long, List<Authority>> menuMap = new HashMap<Long, List<Authority>>();
        for (Authority authority : authorityList)
        {
            Long pid = authority.getParentId();
            if (!menuMap.containsKey(pid))
            {
                List<Authority> list = new ArrayList<Authority>();
                list.add(authority);
                menuMap.put(pid, list);
            }
            else
            {
                menuMap.get(pid).add(authority);
            }
        }
        if (!menuMap.isEmpty())
        {
            
            List<Authority> listAuth = menuMap.get(Long.parseLong("0"));
            // 循环第一级菜单
            for (int i = 0; i < listAuth.size(); i++)
            {
                Authority auth = listAuth.get(i);
                Long id = auth.getId();
                List<Authority> nodeAuthList = menuMap.get(id);
                auth.setAuthoritys(nodeAuthList);
                currentAuthority.add(auth);
            }
        }
        return currentAuthority;
    }
    
    /**
     * 描述：构造菜单对象
     * 作者：张进飞
     * 时间：2018年6月10日 下午4:53:10
     * @param request1
     * @return
     */
    @Deprecated
    private Map<String, List<MenuVo>> getMenus(HttpServletRequest request1)
    {
        CurrentUser user = (CurrentUser) request1.getSession().getAttribute(Constants.USER_LOGIN_FLAG);
        if (null == user)
        {
            return null;
        }
        List<MenuVo> menusVoList = new ArrayList<MenuVo>();
        List<Authority> authorityList = user.getAuthoritys();
        //根据parentId对菜单进行分类
        Map<Long, List<Authority>> menuMap = new HashMap<Long, List<Authority>>();
        for (Authority authority : authorityList)
        {
            Long pid = authority.getParentId();
            if (!menuMap.containsKey(pid))
            {
                List<Authority> list = new ArrayList<Authority>();
                list.add(authority);
                menuMap.put(pid, list);
            }
            else
            {
                menuMap.get(pid).add(authority);
            }
        }
        if (!menuMap.isEmpty())
        {
            
            List<Authority> listAuth = menuMap.get(Long.parseLong("0"));
            // 循环第一级菜单
            for (int i = 0; i < listAuth.size(); i++)
            {
                Authority auth = listAuth.get(i);
                
                MenuVo menuVo = new MenuVo();
                menuVo.setMenuid(auth.getId());
                menuVo.setMenuname(auth.getAuthorityName());
                menuVo.setIcon(auth.getIcon());
                Long id = auth.getId();
                List<Authority> nodeAuthList = menuMap.get(id);
                List<MenuVo> nodeMenuVoList = new ArrayList<MenuVo>();
                // 循环第二级菜单
                for (int l = 0; l < nodeAuthList.size(); l++)
                {
                    Authority nodeAuth = nodeAuthList.get(l);
                    MenuVo nodeMenuVo = new MenuVo();
                    nodeMenuVo.setMenuid(nodeAuth.getId());
                    nodeMenuVo.setMenuname(nodeAuth.getAuthorityName());
                    nodeMenuVo.setIcon(nodeAuth.getIcon());
                    nodeMenuVo.setUrl(nodeAuth.getAuthorityUrl());
                    nodeMenuVoList.add(nodeMenuVo);
                }
                menuVo.setMenus(nodeMenuVoList);
                menusVoList.add(menuVo);
            }
        }
        Map<String, List<MenuVo>> menuResultMap = new HashMap<String, List<MenuVo>>();
        menuResultMap.put("menus", menusVoList);
        return menuResultMap;
    }
    
    /**
     * 描述：根据当前用户获取菜单列表
     * 作者：张进飞
     * 时间：2018年5月20日 上午11:51:46
     * @param user
     * @return
     */
    private String getMenus(CurrentUser user)
    {
        if (null == user)
        {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        List<Authority> authorityList = user.getAuthoritys();
        //根据parentId对菜单进行分类
        Map<Long, List<Authority>> menuMap = new HashMap<Long, List<Authority>>();
        for (Authority authority : authorityList)
        {
            Long pid = authority.getParentId();
            if (!menuMap.containsKey(pid))
            {
                List<Authority> list = new ArrayList<Authority>();
                list.add(authority);
                menuMap.put(pid, list);
            }
            else
            {
                menuMap.get(pid).add(authority);
            }
        }
        
        if (!menuMap.isEmpty())
        {
            List<Authority> listAuth = menuMap.get(Long.parseLong("0"));
            // 循环第一级菜单
            for (int i = 0; i < listAuth.size(); i++)
            {
                Authority auth = listAuth.get(i);
                sb.append("<li id='menu-li-id-" + auth.getId() + "'>");
                sb.append("<p class=\"mb-kind\">");
                sb.append("<i class=\"" + auth.getIcon() + " mb-icon\"></i><label>" + auth.getAuthorityName() + "</label>");
                sb.append("</p>");
                
                sb.append("<div class=\"mb-content\"><ul class=\"mb-detail\">");
                Long id = auth.getId();
                List<Authority> nodeAuthList = menuMap.get(id);
                // 循环第二级菜单
                for (int l = 0; l < nodeAuthList.size(); l++)
                {
                    Authority nodeAuth = nodeAuthList.get(l);
                    sb.append("<li id='" + nodeAuth.getId() + "'><a href=\"javascript:void(0)\" data-target=\"" + nodeAuth.getAuthorityUrl() + "\">" + nodeAuth.getAuthorityName() + "</a></li>");
                }
                sb.append("</ul></div>");
                sb.append("</li>");
            }
        }
        return sb.toString();
    }
}
